﻿using Autofac.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Tauren.Business.Infrastructure;
using Tauren.Business.Infrastructure.Common;
using Tauren.Business.Service.UnitInfo;

namespace Tauren.Business.Core.Controllers
{
    /// <summary>
    /// 单位信息
    /// </summary>
    [Route("api/unitinfo/v{version:apiVersion}")]
    [ApiVersion("1.0")]
    [ApiVersion("2.0")]
    [ApiController]
    public class UnitInfoController : ControllerBase
    {
        private IUnitInfoService service { get; set; }

        public UnitInfoController(IEnumerable<IUnitInfoService> services)
        {
            this.service = services.FirstOrDefault(o => o.Version == 1.0);
            if (this.service == null) throw new ApiVersionException("不支持当前版本");
        }
        /// <summary>
        /// 新增单位信息
        /// </summary>
        /// <param name="input">单位信息</param>
        /// <returns></returns>
        [HttpPost, Route("add")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Add([FromBody] Entity.Input.Unitinfo_Input input) => service.Add(input).ToJsonResult();


        /// <summary>
        /// 编辑单位信息
        /// </summary>
        /// <param name="input">单位信息</param>
        /// <returns></returns>
        [HttpPost, Route("edit")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Editor([FromBody] Entity.Input.Unitinfo_Input input) => service.Edit(input).ToJsonResult();

        /// <summary>
        /// 获取单个单位信息数据
        /// </summary>
        /// <param name="id">单位实体id</param>
        /// <returns></returns>
        [HttpPost, Route("get/{id}")]
        public IActionResult Get([FromRoute] int id) => service.FirstOrDefault(id).ToJsonResult();

        /// <summary>
        /// 删除单位信息
        /// </summary>
        /// <param name="id">单位实体id</param>
        /// <returns></returns>
        [HttpPost, Route("del/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Delete([FromRoute] int id) => service.Delete(id).ToJsonResult();


        /// <summary>
        /// 单位信息
        /// </summary> 
        /// <param name="code">单位编码</param>
        /// <returns></returns>
        [HttpPost, Route("code/{code}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult GetByCode([FromRoute] string code) => service.GetByCode(code).ToJsonResult();

        /// <summary>
        /// 单位信息-多个
        /// </summary> 
        /// <param name="code">单位编码</param>
        /// <returns></returns>
        [HttpPost, Route("codes")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult GetByCodes([FromBody] string[] code) => service.GetByCodes(code).ToJsonResult();



        /// <summary>
        /// 获取所有数据
        /// </summary> 
        /// <returns></returns>
        [HttpPost, Route("all")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult All() => service.All().ToJsonResult();

        /// <summary>
        /// 分页获取 单位信息列表
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        [HttpPost, Route("pages")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Pages([FromBody] Entity.Global.QueryModel queryModel) => service.Pages(queryModel).ToJsonResult();


        /// <summary>
        /// 启用??
        /// </summary>
        /// <param name="id">单位ID</param> 
        /// <returns></returns>
        [HttpPost, Route("enable/{id:int}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Enable(int id) => service.Enable(id).ToJsonResult();
        /// <summary>
        /// 获取字段数据
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost, Route("config/{type}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Config([FromRoute] string type) => service.Config(type).ToJsonResult(true);

        /// <summary>
        /// 公司树
        /// </summary> 
        /// <returns></returns>
        [HttpPost, Route("tree")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Tree() => service.Tree().ToJsonResult();
    }
}
